package com.fuyao.cloud.admin.flow.config;

import com.fuyao.cloud.admin.flow.listener.GlobalProcistEndListener;
import com.fuyao.cloud.admin.flow.listener.GlobalProcistStartListener;
import com.fuyao.cloud.admin.flow.listener.MyFlowableEngineEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

@Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private SpringProcessEngineConfiguration configuration;
    @Autowired
    private GlobalProcistEndListener globalProcistEndListener;
    @Autowired
    private GlobalProcistStartListener globalProcistStartListener;

    @Autowired
    private MyFlowableEngineEventListener myFlowableEngineEventListener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        //添加流程实例创建全局监听
        dispatcher.addEventListener(globalProcistStartListener, FlowableEngineEventType.PROCESS_STARTED);
        //添加流程实例结束全局监听
        dispatcher.addEventListener(globalProcistEndListener, FlowableEngineEventType.PROCESS_COMPLETED);
        //
        dispatcher.addEventListener(myFlowableEngineEventListener);
    }
}
